﻿var threadsData = '';
var moderator = false;
var interval = 7000;
var timer = setTimeout("dataChanged()", interval);
var pageType = "Threads";
var forumName;

var index = -1; // index of the selected item
function dataChanged() {
    var username = getUsername();
    WebService.dataChanged(username, pageType, getThreads);
}

function getThreads(dataHasChanged) {
    if (dataHasChanged == true) {
        //still need to pasrse url to get the name of the forum
        WebService.getSThreads(forumName, onSuccessGetThreads, fail);
    }
    timer = setTimeout("dataChanged()", interval);
}
function fail() {
    alert("Failed to load the threads. try to refresh");
}
function onSuccessGetThreads(result) {
    //change threads table according to result
    threadsData = result;
    createTable();
}

window.onload = function () {
    if (document.getElementById("moderator").title == "True") {
        document.getElementById("DeleteButton").style.display = "";
        document.getElementById("EditButton").style.display = "";
    }
    var parameters;

    var Variables = document.URL.split("Topic=");
    forumName = Variables[1];
    getThreads(true);
}

function createTable() {
    var table = document.getElementById("ThreadsTable");
    for (var i = table.rows.length; i > 1; i--) {
        table.deleteRow(i - 1);
    }
    for (var i = 0; i < threadsData.length; i++) {
        addRowToThreadsTable(threadsData[i]);
    }
}

function refreshThreadsTable() {
    getThreads(true);
    createTable();
}
function addRowToThreadsTable(thread) {
    var table = document.getElementById("ThreadsTable");

    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var checkbox = row.insertCell(0);
    var checkBoxElement = document.createElement("input");
    checkBoxElement.type = "checkbox";
    checkbox.appendChild(checkBoxElement);

    var header = row.insertCell(1);
    var element1 = document.createElement("a");
    element1.setAttribute("href", "Posts.aspx?Topic=" + thread.Forum + "&Thread=" + thread.Header);
    var linkText = document.createTextNode(thread.Header);
    element1.appendChild(linkText);
    header.appendChild(element1);

    var reply = row.insertCell(2);
    reply.innerHTML = thread.NumOfReplies;
    
    var views = row.insertCell(3);
    views.innerHTML = thread.NumOfViews;

    var creator = row.insertCell(4);
    creator.innerHTML = thread.Creator;

    var publish = row.insertCell(5);
    publish.innerHTML = thread.PublishDate;

    var LastDate = row.insertCell(6);
    LastDate.innerHTML = thread.LastDate;
}
function getSelectedItemsCount() {
    var count = 0;
    var table = document.getElementById("ThreadsTable");
    for (var i = 0; i < table.rows.length; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if (null != chkbox && true == chkbox.checked) {
            index = i-1;
            count++;
        }
    }
    return count;
}

function addClicked() {
    showElements(true);
    document.getElementById("AddThreadButton").style.visibility = "visible";
}

function checkIfChooseThreads() {
    var numOfIndexes = getSelectedItemsCount();
    if (numOfIndexes == 0) {
        var error = document.getElementById("MainContent_ErrorLabel");
        error.innerHTML = "Please Choose thread";
        error.style.display = "";
        return false;
    }
    else if (numOfIndexes > 1) {
        var error = document.getElementById("MainContent_ErrorLabel");
        error.innerHTML = "You chose more than one thread";
        error.style.display = "";
        return false;
    }
    return true;
}


function editClicked(isModerator, username) {
    document.getElementById("MainContent_ErrorLabel").style.display = "none";
    if (checkIfChooseThreads() == false) {
        return;
    }
    if (username != threadsData[index].Creator) {
        alert("Cannot edit thread that you didn't create");
        return;
    }

    showElements(true);
    document.getElementById("EditThreadButton").style.visibility = "visible";
    document.getElementById("MainContent_AddThreadHeaderTextBox").value = threadsData[index].Header;
    document.getElementById("MainContent_AddThreadBodyTextBox").value = threadsData[index].Body;
}

function deleteThread(isModerator, username) {
    if (isModerator == "False") {
        var error = document.getElementById("MainContent_ErrorLabel");
        error.innerHTML = "Only moderators can delete threads";
        error.style.display = "";
        return;
    }
    if (checkIfChooseThreads() == false) {
        return;
    }
    document.getElementById("MainContent_ErrorLabel").style.display = "none";
    // call to delete thread

    WebService.DeleteThread(username, threadsData[index].Forum, threadsData[index].Header, AddEditDeleteThreadSuccess, deleteError);
}

function deleteError(result) {
    alert(result);
}

function cancelClicked() {
    showElements(false);
}
function addEditThreadClick(username, isAdd) {
    var headerText = document.getElementById("MainContent_AddThreadHeaderTextBox").value;
    var bodyText = document.getElementById("MainContent_AddThreadBodyTextBox").value;
    if (addEditCheckIfNotNull(headerText, bodyText))
        showElements(false);
    else
        return;
    if (isAdd == true) {
        WebService.AddThread(username, forumName, headerText, bodyText, AddEditDeleteThreadSuccess, errorAddEdit);
    }
    else {
        WebService.EditThread(username, forumName, threadsData[index].Header, headerText, bodyText, AddEditDeleteThreadSuccess, errorAddEdit)
    }
//    refreshThreadsTable();
}
function AddEditDeleteThreadSuccess(result) {

    //alert("Success!!!");
    refreshThreadsTable();
    alert(result);
}
function errorAddEdit(result) {
    alert(result);
}

function addEditCheckIfNotNull(headerText, bodyText) {
    var errorlabel = document.getElementById("MainContent_AddThreadErrorLabel");
    if (!(headerText.length)) {
        errorlabel.innerHTML = "Please enter header!!!";
        errorlabel.style.display = "inline";
        return false;
    }
    if (!(bodyText.length)) {
        errorlabel.innerHTML = "Please enter body!!!";
        errorlabel.style.display = "inline";
        return false;
    }
    errorlabel.style.display = "none";
    return true;
}

function addrow(header, username, forumname) {
    var grd = document.getElementById("MainContent_ThreadsGridView");
    var tbod = grd.rows[0].parentNode;
    var newRow = grd.rows[grd.rows.length - 1].cloneNode(true);
    var cell0 = "<a href=\"Posts.aspx?Topic=" + forumname + "&amp;Thread=" + header + "\">" + header + "</a>";
    newRow.cells[0].innerHTML = cell0;
    newRow.cells[1].innerHTML = 0;
    newRow.cells[2].innerHTML = 0;
    newRow.cells[3].innerHTML = username;
    newRow.cells[4].innerHTML = new Date();
    newRow.cells[5].innerHTML = new Date();
    tbod.appendChild(newRow);
}
 

function showElements(show) {
    if (show) {
        document.getElementById("MainContent_AddThreadHeaderTextBox").value = "";
        document.getElementById("MainContent_AddThreadBodyTextBox").value = "";
        document.getElementById("MainContent_AddThreadHeaderLabel").style.display = "";
        document.getElementById("MainContent_AddThreadHeaderTextBox").style.display = "";
        document.getElementById("MainContent_AddThreadBodyLabel").style.display = "";
        document.getElementById("MainContent_AddThreadBodyTextBox").style.display = "";
        document.getElementById("MainContent_AddThreadErrorLabel").style.display = "none";
        document.getElementById("CancelButton").style.visibility = "visible";
        document.getElementById("AddButton").style.visibility = "hidden";
        document.getElementById("EditButton").style.visibility = "hidden";
        document.getElementById("DeleteButton").style.visibility = "hidden";
    }
    else {
        document.getElementById("AddThreadButton").style.visibility = "hidden";
        document.getElementById("EditThreadButton").style.visibility = "hidden";
        document.getElementById("MainContent_AddThreadHeaderLabel").style.display = "none";
        document.getElementById("MainContent_AddThreadHeaderTextBox").style.display = "none";
        document.getElementById("MainContent_AddThreadBodyLabel").style.display = "none";
        document.getElementById("MainContent_AddThreadBodyTextBox").style.display = "none";
        document.getElementById("MainContent_AddThreadErrorLabel").style.display = "none";
        document.getElementById("CancelButton").style.visibility = "hidden";
        document.getElementById("AddButton").style.visibility = "visible";
        document.getElementById("EditButton").style.visibility = "visible";
        document.getElementById("DeleteButton").style.visibility = "visible";
        
    }
    document.getElementById("MainContent_ErrorLabel").style.display = "none";
}
    